/* Application Features
- Ability to send broadcast messages
- Ability to set register values
- Ability to query register values
- Ability to read analog registers
- Ability to calculate crc 
- Ability to acknowledge message transfers
- Ability to send multiple messages.

- Message Properties (Max Message size)
-- Source Address (1 Byte )
-- Destination Address (1 Byte)
-- 
-- Payload
-- 

Notes:
1.) source address is only available in the msg header. In effect all
	messages are broadcast messages. header supports responding to the 
	sender only. 
2.) Since the destination address is not in the msg header, the receiver
	does not know if the message is intended for a particular node. 
3.) Message header could be altered to include both source and destination. 
4.) Short term solution... include address information in payload. 

	
*/
//
//
// 
//#include <FoxNet.h>
#include <JeeLib.h>
#include <util/crc16.h>
#include <util/parity.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <mesh1_cfg.h>
//
#define DATAFLASH 1 	// Check for presence of Dataflash on JeeNode Hardware
#define FLASH_MBIT 16 	// Suppoer for various dataflash sizes: 4/8/16/Mbit
#define COLLECT 0x20 	// Msg delivery mode  

// Define system globals
MilliTimer sendTimer;
byte needToSend;

char payload[] = "Hello!";
//


static unsigned long now() {
	//return relative time since startup
	// Note: current logic only supports 49-Day timer resolution
	//	before an overflow condition occurs 
	// TODO: FIXME
	return millis() / 1000;
}


typedef struct {
	byte  	destAddr;
	byte	msgType;
	byte	portA;
	byte	portB;
	byte	portC;
	byte	portD; 
} Msg;

// define the standard message payload struct
// Each message type the collect
static Msg msg;

void setup() {
	// Set serial interface speed from mesh1_cfg.h
	Serial.begin(TTY_PORT_SPEED);
	// Print startup message
	Serial.println("\nMESH1");
	// initialize radio with preconfigured network values. 
   	 // values stored in mesh1_cfg.h
	rf12_initialize(NODE_ID, RF12_BAND, GROUP_ID);
}
/*
bool msgCheckNode () {
	// Read message payload determine 
}
*/

void loop() {
	// Was a message received, is it for me?
	if (rf12_recvDone() && rf12_crc == 0 && RF12_HDR_MASK & rf12_hdr == NODE_ID && RF12_HDR_DST == 1 ) {
	//if (rf12_recvDone() && rf12_crc == 0  ) {
		// valid message received, send context to msgProcessor.
		
		Serial.print("Msg Header: ");	
		Serial.println(rf12_hdr, BIN);	
		// is the message for me?
		
		if (RF12_HDR_MASK & rf12_hdr == NODE_ID && RF12_HDR_DST == 1) {
			Serial.println("Message is for ME!!!");
		} else if (RF12_HDR_MASK & rf12_hdr == NODE_ID && RF12_HDR_ACK == 1) {
			Serial.println("Acknowledgement for ME.....");
		} else {
			Serial.println("OK, WTF");
		}
		
		// Cast a valid message into a message structure...
		// Contiune processing...		
		const Msg* msg_r = (const Msg*) rf12_data;			


		//Serial.print("[ ");
		//Serial.print(msg_r->destAddr);
		//Serial.print(" ] Received message from NodeId: ");
		/*
		for (byte i = 0; i < rf12_len; i++ ) {
			Serial.print(rf12_data[1]);
		}
		*/
		// Get the reply to address from the header 
		//Serial.print( RF12_HDR_MASK & rf12_hdr);
		//Serial.print(" --");
		//Serial.println();
		// add pause for settling
		delay(10);
				
	}


	if (sendTimer.poll(3000)) {
		// Check timer to determine if its time to send msg
		needToSend = 1;
	}


	if (needToSend && rf12_canSend()) {
		needToSend = 0;
		// send message to specific node
		msg.destAddr = 3; // hard coded for testing. 
		//msg.payLoad = "Hello Number 3"; // hard coded for testing. 
		Serial.println("Sending to Node 3");			
		rf12_sendStart(msg.destAddr, &msg, sizeof msg);
		delay(10);
		// send message to specific node
		msg.destAddr = 25; // hard coded for testing. 
		//msg.payLoad = "Hello Number 25"; // hard coded for testing. 
		Serial.println("Sending to Node 25");			
		rf12_sendStart(msg.destAddr, &msg, sizeof msg);
		delay(10);
		//strcpy(msg.payLoad,  payload);
		

		//Serial.print("Struct nodeId-> ");
		//Serial.println(msg.srcAddr);
		//Serial.print("Struct Message -> ");
		//Serial.println(msg.payLoad);
		/*	
		Serial.println("Sending to Node 3");			
		rf12_sendStart(3, payload, sizeof payload);
		delay(10);
		Serial.println("Sending to Node 25");			
		rf12_sendStart(msg., payload, sizeof payload);
		delay(10);
		*/
		//Serial.println("Sending to Broadcast.........");			
		//rf12_sendStart(0, payload, sizeof payload);
		//delay(340);
		//rf12_sendStart(0, &msg, sizeof msg);
	}	


	/*
	while (!rf12_canSend())
			// receive messages while transmission channel is busy.
    		rf12_recvDone();
  	// send an empty packet to one specific pollee
  	rf12_sendStart(RF12_HDR_ACK | RF12_HDR_DST | nextId, 0, 0);
  	// wait up to 10 milliseconds for a reply
  	timer.set(10);
  	while (!timer.poll())
        if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof (Payload)) {
      		// got a good ACK packet, print out its contents
            const Payload* p = (const Payload*) rf12_data;
            Serial.print((word) p->node);
            Serial.print(": ");
            Serial.println(p->time);
            break;
        }

	*/


}
